import QtQuick 2.0
import Toou2D 1.0
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import Qt.labs.platform 1.0
import cn.conchit 1.0


Item {
    id: label_item
    width: parent.width / 8;
    height: parent.height / 3;

    signal labelSelected(var newLabel);

    function setSavePath(currentFolder) {
        lm.setSavePath(currentFolder);
        labelList_body.initLabel();
    }


    function labelSelectChange(step) {
        var index = labelList_body.currentIndex + step
        if (index < 0 || index >= lm.rowCount()) return;
        if (labelList_body.lastCurrentIndex != -1) {
            if(labelList_body.itemAtIndex(labelList_body.lastCurrentIndex)){
                labelList_body.itemAtIndex(labelList_body.lastCurrentIndex).color = "#9DD6E5"
            }
        }
        labelList_body.currentIndex = index
        labelList_body.itemAtIndex(labelList_body.currentIndex).color = "#196E6C"
        labelList_body.lastCurrentIndex = index
        console.log(index + "================")
        var retLabel = JSON.parse(lm.get(index))
        console.log(retLabel.retStr)
        if(retLabel.retVal === 1) {
            labelSelected(retLabel.retObj)
        }
    }

    ColorDialog {
        id: colorDialog
        title: qsTr("选择颜色")
        color: "#000000"
    }

    //标签添加对话框
    TDialog {
        id:dialog_input
        titleText: "标签添加";
        closeable: false
        modalclose:false;
        buttons: [
            TDialogButton{
                label.text: "取消"
                label.font.pixelSize: TPixelSizePreset.PH5
            },
            TDialogButton{
                label.text: "取色"

                label.font.pixelSize: TPixelSizePreset.PH5
                lighter:true
            },
            TDialogButton{
                label.text: "确认"
                label.font.bold: true;
                label.font.pixelSize: TPixelSizePreset.PH5
                label.color: "#46A0FC"
                lighter:true
            }
        ]
        Rectangle{
            id: dialog_input_rect
            property alias text: input.text
            width: 260;
            height: 45;
            color: colorDialog.color
            TInputField{
                id:input
                width: 255;
                height: 40;
                anchors.centerIn: parent;
            }
        }



        onTriggered: function(button, item) {
            if(button.label.text === "确认" && item.text){
                var jsonStr = lm.append(item.text.trim(), colorDialog.color);
                var JsonObject= JSON.parse(jsonStr);
                if (JsonObject.retVal === 1) {
                    TToast.showSuccess(JsonObject.retStr + "  " + item.text + "  :" + colorDialog.color, TTimePreset.ShortTime2s);
                } else {
                    TToast.showError(JsonObject.retStr + "  " + item.text + "  :" + colorDialog.color, TTimePreset.ShortTime2s);
                }
                hideAndClose();
            } else if(button.label.text === "取色") {
                colorDialog.open();
            } else {
                TToast.showInfo("已取消",TTimePreset.ShortTime2s)
                hideAndClose();
            }

        }

    }

    //标签删除对话框
    TDialog{
        id:dialog_tip
        titleText: "标签删除";
        contentCenter:false;
        contentText: "123"


        buttons: [
            TDialogButton{
                label.text: "取消"
                label.font.pixelSize: TPixelSizePreset.PH5
            },
            TDialogButton{
                label.text: "确认"
                label.font.bold: true;
                label.font.pixelSize: TPixelSizePreset.PH5
                label.color: "#46A0FC"
                lighter:true
            }
        ]

        Item{
            width: 260;
            height: 40

            TLabel{
                anchors.left: parent.left;
                anchors.leftMargin: 20;
                anchors.verticalCenter: parent.verticalCenter;
                text: dialog_tip.contentText
            }
        }

        onTriggered: function(button) {

            if(button.label.text === "确认"){
                var jsonStr = lm.remove(dialog_tip.contentText)
                var JsonObject= JSON.parse(jsonStr);
                if (JsonObject.retVal === 1) {
                    TToast.showSuccess(JsonObject.retStr + " " + dialog_tip.contentText , TTimePreset.ShortTime2s);
                } else {
                    TToast.showError(JsonObject.retStr + "  " + dialog_tip.contentText , TTimePreset.ShortTime2s);
                }
            }
            else{
                TToast.showInfo("已取消",TTimePreset.ShortTime2s)
            }
            hideAndClose();
        }
    }

    //标签列表
    TRectangle{
        id: labelList
        width: parent.width
        height: parent.height
        color: "#196E6C";
        radius: 4
        theme.enabled: false;
        focus: true

        TIconButton {
            icon.source: TAwesomeType.FA_plus;
            icon.color: "#FCFCFC"
            backgroundComponent: null;
            label.text: ""
            onClicked: {
                colorDialog.color = "#000000"
                dialog_input.open()
            }
        }

        TLabel{
            id: labelList_head
            x:10;
            y:10;
            color: "#FFF"
            text: "标签列表" ;
            height: 20
            anchors.horizontalCenter: parent.horizontalCenter;
            theme.enabled: false;
        }
        TRectangle{
            anchors.top: labelList_head.bottom
            anchors.bottom: parent.bottom;
            width: parent.width
            color: "#002B36";
            theme.enabled: false;


            ListView {
                id: labelList_body
                anchors.fill: parent
                anchors.margins: 2
                clip: true
                model: LabelModel{id: lm}

                delegate: labelDelegate

                spacing: 5
                currentIndex: -1
                focus: true
                property int lastCurrentIndex: -1

                function initLabel() {
                    labelList_body.currentIndex = 0
                    labelList_body.lastCurrentIndex = 0
                    if(labelList_body.itemAtIndex(0) !== null) {
                        labelList_body.itemAtIndex(0).color = "#196E6C"
                    }
                    var retLabel = JSON.parse(lm.get(0))
                    if(retLabel.retVal === 1) {
                        labelSelected(retLabel.retObj)
                    }
                }




            }

            Component {
                id: labelDelegate

                Rectangle {
                    width: ListView.view.width
                    height: 30
                    color: "#9DD6E5"
                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        onPressed: {
                            if (labelList_body.lastCurrentIndex != -1) {
                                if(labelList_body.itemAtIndex(labelList_body.lastCurrentIndex)){
                                    labelList_body.itemAtIndex(labelList_body.lastCurrentIndex).color = "#9DD6E5"
                                }
                            }
                            labelList_body.currentIndex = index
                            color = "#196E6C"
                            labelList_body.lastCurrentIndex = index
                            var retLabel = JSON.parse(lm.get(index))
                            if(retLabel.retVal === 1) {
                                labelSelected(retLabel.retObj)
                            }
                        }
                        onEntered: {
                            if (labelList_body.currentIndex === index) return
                            color = "#196E6C"
                        }
                        onExited: {
                             if (labelList_body.currentIndex === index) return
                             color = "#9DD6E5"
                        }
                    }
                    Text {
                        id: label_text
                        anchors.left : parent.left
                        anchors.verticalCenter: parent.verticalCenter
                        font.pixelSize: 16
                        text:  seq + " : " + labelName
                    }

                    Rectangle{
                        x: label_text.x + label_text.width + 16
                        width:24
                        height:16
                        radius: 8
                        color: labelColor

                        anchors.verticalCenter: parent.verticalCenter
                    }

                    TIconButton {
                        anchors.right : parent.right
                        anchors.verticalCenter: parent.verticalCenter
                        icon.source: TAwesomeType.FA_minus;
                        backgroundComponent: null;
                        //icon.color: "black"
                        label.text: ""
                        onClicked: {
                            dialog_tip.contentText = labelName
                            dialog_tip.open()
                        }
                    }
                }

            }
        }





    }

    Component.onCompleted: {
        labelList_body.initLabel();
    }



}
